﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DotNet.Utilities;
using PropertyChanged;
using WPFPractice.Utils;
using WPFTemplateLib.WpfHelpers;

namespace WPFPractice.Windows
{
    public partial class WinInputMethod : Window
    {
        private VmInputMethod _vm;

        public WinInputMethod()
        {
            InitializeComponent();
            DataContext = _vm = new VmInputMethod();

            //TabTipAutomation.IgnoreHardwareKeyboard = HardwareKeyboardIgnoreOptions.IgnoreAll;
            //TabTipAutomation.BindTo<TextBox>();
        }

        private void CbInputScope_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            InputScope inputScope = InputMethod.GetInputScope(TbTest);
            _vm.TbTestCurrentInputScope = (inputScope?.Names?[0] as InputScopeName)?.NameValue.ToString()!;
        }
    }

    [AddINotifyPropertyChangedInterface]
    public class VmInputMethod : BindableBase
    {
        public VmInputMethod()
        {
            SetCommandMethod();

            InitValue();
        }

        /// <summary>
        /// 数据初始化
        /// </summary>
        private void InitValue()
        {
            InputScopeNameList = EnumExtension.GetEnumList<InputScopeNameValue>().OrderBy(x=>x.ToString()).ToList();
            InputMethodStateList = EnumExtension.GetEnumList<InputMethodState>();
            ImeConversionModeList = EnumExtension.GetEnumList<ImeConversionModeValues>();
            ImeSentenceModeList = EnumExtension.GetEnumList<ImeSentenceModeValues>();

            SelectedInputScopeName = InputScopeNameValue.Default;
            SelectedInputMethodState = InputMethodState.On;
            SelectedImeConversionMode = ImeConversionModeValues.Native;
            SelectedImeSentenceMode = ImeSentenceModeValues.None;

            LangList.Add("");
            var languages = InputLanguageManager.Current.AvailableInputLanguages;
            if (languages != null)
            {
                foreach (CultureInfo language in languages)
                {
                    LangList.Add(language.Name);
                }
            }
        }

        /// <summary>
        /// 命令方法赋值(在构造函数中调用)
        /// </summary>
        private void SetCommandMethod()
        {
            TbPreviewMouseDownCmd ??= new RelayCommand<bool>(b =>
            {
                OskOpr.SwitchLocalLanguage();

                if (b)
                {
                    //var processes = Process.GetProcessesByName("TabTip");
                    //foreach (var p in processes)
                    //{
                    //    p.Kill();
                    //}

                    TabTipHelper.ShowTabTipSimple();
                }
            });
        }

        #region 绑定

        public List<InputScopeNameValue> InputScopeNameList { get; set; }

        public InputScopeNameValue SelectedInputScopeName { get; set; }

        public string TbTestCurrentInputScope { get; set; }

        public List<InputMethodState> InputMethodStateList { get; set; }

        public InputMethodState SelectedInputMethodState { get; set; }

        public List<ImeConversionModeValues> ImeConversionModeList { get; set; }

        public ImeConversionModeValues SelectedImeConversionMode { get; set; }

        public List<ImeSentenceModeValues> ImeSentenceModeList { get; set; }

        public ImeSentenceModeValues SelectedImeSentenceMode { get; set; }

        public ObservableCollection<string> LangList { get; set; } = new ObservableCollection<string>();

        #endregion

        #region 命令

        public ICommand TbPreviewMouseDownCmd { get; set; }

        #endregion
    }
}
